<?php

/**
 * search actions.
 *
 * @package    PHOTO_GALLERY
 * @subpackage search
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class searchActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        $numImages = $request->getParameter('numImages', 10);
        $page = $request->getParameter('page', 1);
        $this->pager = new sfDoctrinePager('Images', $numImages);
        $this->pager->setQuery(ImagesTable::getAll($this->getUser()->getAttribute('userId')));
        $this->pager->setPage($page);
        $this->pager->init();
    }

    public function executeAutoComplete(sfWebRequest $request) {
        $owner = $request->getParameter('ownerName');
        $ret = UsersLoginTable::checkAutoComplete($owner);
        $this->users = array();
        foreach ($ret as $r) {
            $this->users[] = $r['username'];
        }
        //var_dump($users);
    }

    public function executeAdvSearch(sfWebRequest $request) {
        $imgName        =   $request->getParameter('imgName');
        $albumName      =   $request->getParameter('albumName');
        $ownerName      =   $request->getParameter('ownerName');
        $dateTo         =   $request->getParameter('dateTo');
        $dateFrom       =   $request->getParameter('dateFrom');
        
        $q      =   ImagesTable::advSearch($imgName, $ownerName, $albumName, $dateFrom, $dateTo);
        $numImages = $request->getParameter('numImages', 10);
        $page = $request->getParameter('page', 1);
        $this->pager = new sfDoctrinePager('Images', $numImages);
        $this->pager->setQuery($q);
        $this->pager->setPage($page);
        $this->pager->init();
        return $this->renderPartial('search/searchResult', array('pager'=>  $this->pager));
    }
    
    public function executeSort(sfWebRequest $request){
        $imgName        =   $request->getParameter('imgName');
        $albumName      =   $request->getParameter('albumName');
        $ownerName      =   $request->getParameter('ownerName');
        $dateTo         =   $request->getParameter('dateTo');
        $dateFrom       =   $request->getParameter('dateFrom');
        $sorter         =   $request->getParameter('sorter');
        $numImages      =   $request->getParameter('numImages', 10);
        
        $q      =   ImagesTable::sort($imgName, $ownerName, $albumName, $dateFrom, $dateTo, $sorter);
        
        $page = $request->getParameter('page', 1);
        $this->pager = new sfDoctrinePager('Images', $numImages);
        $this->pager->setQuery($q);
        $this->pager->setPage($page);
        $this->pager->init();
        return $this->renderPartial('search/searchResult', array('pager'=>  $this->pager));
    }

    public function executesSimpleSearch(sfWebRequest $request) {
        var_dump(ImagesTable::simpleSearch('z', 'z', 'z', 'a', null, NULL));
        $this->setTemplate('index');
    }

}
